package cn.wolfcode.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
/*@RequestMapping("/login")*/
public class LoginController {
    @Autowired
    IEmployeeService employeeService;
    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult empLogin(String username, String password, HttpSession session){
             JsonResult result =new JsonResult();

            Employee employee = employeeService.empLogin(username, password);
            session.setAttribute("USER_IN_SESSION",employee);
            List<Permission> permissionList = employeeService.selectPermissionById(employee.getId());
            session.setAttribute("PERMISSION_IN_SESSION",permissionList);
            result.setSuccess(true);


        return result;
    }
@RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    public String nopermission(){
        return "common/nopermission";
    }
@RequestMapping("/error")
    public String error(){
        return "common/error";
    }
}
